package com.financing.platform.web;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.dto.FaUserDTO;
import com.financing.platform.dto.FaUserVisitDTO;
import com.financing.platform.service.IFaUserService;
import com.financing.platform.service.IFaUserVisitService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;
import com.financing.platform.util.StringUtil;

@Controller
public class FAUserVisitAction extends BaseAction{
	
	@Autowired
	private IFaUserVisitService faUserVisitServiceImpl;
	
	@Autowired
	private IFaUserService faUserServiceImpl;
	
	@RequestMapping("/queryFaUserVisit.shtml")
	@ResponseBody
	public Map<String,Object> queryFaUserVisit(HttpServletRequest request,
			HttpServletResponse response,FaUserVisitDTO faUserVisitDTO) {
		
			List<FaUserVisitDTO> faUserVisitList = faUserVisitServiceImpl.queryFaUserVisit(faUserVisitDTO);
			
			Map<String,Object> result = new HashMap<String,Object>();
			
			result.put("faUserVisitList", faUserVisitList);
			result.put(Dict.result, Dict.result_success);
			
			return result;
	}
	
	
	
	@RequestMapping("/addFaUserVisit.shtml")
	@ResponseBody
	public Map<String,Object> addFaUserVisit(HttpServletRequest request,
			HttpServletResponse response,FaUserVisitDTO faUserVisitDTO) {
		
			Map<String,Object> result  = new HashMap<String,Object>();
			
			DTOdateSet.setCreateInfo(faUserVisitDTO, request.getSession());
			DTOdateSet.setUpdateInfo(faUserVisitDTO, request.getSession());
			faUserVisitDTO.setVisitPerson(faUserVisitDTO.getCreateBy());
			
			try {
				faUserVisitServiceImpl.addFaUserVisit(faUserVisitDTO);
				result.put(Dict.result, Dict.result_success);
			} catch (SQLException e) {
				log.error("add fa user visit error!",e);
				result.put(Dict.result, Dict.result_error);
			}
		return result;
	}
	
	@RequestMapping("/updateADFaUser.shtml")
	@ResponseBody
	public Map<String,Object> updateADFaUser(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String,Object> result  = new HashMap<String,Object>();
		String faId = request.getParameter("faId");
		String	adValid = request.getParameter("adValid");
		FaUserDTO faUserDTO = new FaUserDTO();
		faUserDTO.setId(faId);
		faUserDTO = faUserServiceImpl.queryFaUserInfo(faUserDTO);
		
		log.info(faUserDTO.getMediaSource());
		//注册来源不亿起发 (sourceCode 不为-2)则不进行任何处理
		if(!"YQF".equals(faUserDTO.getMediaSource()) || "1".equals(faUserDTO.getIsValid()) || "0".equals(faUserDTO.getIsValid())){
			result.put(Dict.result, Dict.result_error);
			log.error("fa source Code error!");
			return result;
		}
		
		if(faUserDTO.getIsValid() == null || faUserDTO.getIsValid().equals(Dict.isValid_unknown)){
			faUserDTO.setIsValid(adValid);
		}
		DTOdateSet.setUpdateInfo(faUserDTO, request.getSession());
		try {
			faUserServiceImpl.updateFaUserADInfo(faUserDTO);
		} catch (SQLException e) {
			log.error("update fa user info error!",e);
			result.put(Dict.result, Dict.result_error);
		}
		
		//有效性校验通过 通知亿起发
		if(adValid.equals(Dict.isValid_pass) ){
			URL url = null;
			try {
				 url = new URL("http://o.yiqifa.com/servlet/handleCpaIn?cid=17987&wi="+faUserDTO.getRemark()+"&ana=reg&an="+faUserDTO.getId()+"&ct=basic&ta=1&sd="+URLEncoder.encode(StringUtil.dateNowStr("yyyy-MM-dd HH:mm:ss")));
				 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			     connection.connect();
			     InputStream in = connection.getInputStream();
			     BufferedReader breader = new BufferedReader(new InputStreamReader(in , "GBK"));
			     log.info("url response is" + breader.readLine());
			} catch (Exception e1) {
				log.error("request url"+url+"error",e1);
			}
		}
		
		result.put(Dict.result, Dict.result_success);
		return result;
	}
	
	public static void main(String[] args) throws Exception {
	//	URL url = new URL("http://o.yiqifa.com/servlet/handleCpaIn?cid=tset");
		URL url = new URL("http://o.yiqifa.com/servlet/handleCpaIn?cid=17987&wi="+"ws"+"&ana=reg&an="+"fdsa"+"&ct=basic&ta=1&sd="+URLEncoder.encode(StringUtil.dateNowStr("yyyy-MM-dd HH:mm:ss")));
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.connect();
        InputStream in = connection.getInputStream();
        java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in , "GBK"));
        String str =breader.readLine();
        while(str != null){
            System.out.println(str);
            str=breader.readLine();
        }
	}
}
